﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace pcsse.BBS
{
    public partial class ShowSignlist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ID"] != null)
            {
                if (!IsPostBack)
                {
                    DropDownList1.DataSource = CreateDropListSource();
                    DropDownList1.DataTextField = "BR_NAME";
                    DropDownList1.DataValueField = "BR_NAME";
                    DropDownList1.DataBind();
                    FillDropList2();
                }
            }
            else
                Response.Redirect("..\\");
        }

        protected DataTable CreateDropListSource()
        {
            db_base db = db_base.getDBhandle();
            SqlCommand cmd = new SqlCommand();
            if (Convert.ToInt32(Session["Priority"]) == 3)
            {
                cmd.CommandText = "SELECT BR_NAME FROM T_BRANCH,T_PARTY WHERE PERSONID=@ID AND BRANCH_ID=T_BRANCH.ID";
                cmd.Parameters.Add("@ID", Session["ID"].ToString());
                DataTable dt = db.GetDataTableByCmd(cmd);
                //DataView dv = new DataView(dt);
                return dt;
            }
            if (Convert.ToInt32(Session["Priority"]) == 4)
            {
                cmd.CommandText = "SELECT BR_NAME FROM T_BRANCH";
                DataTable dt2 = db.GetDataTableByCmd(cmd);
                //DataView dv2 = new DataView(dt2);
                return dt2;
            }
            return null;
        }



        protected void FillDropList2()
        {
            db_base db = db_base.getDBhandle();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT SUBJECT FROM T_SUBJECT";
            DataTable dt = db.GetDataTableByCmd(cmd);
            DropDownList2.DataSource = dt;
            DropDownList2.DataTextField = "SUBJECT";
            DropDownList2.DataValueField = "SUBJECT";
            DropDownList2.DataBind();
        }

        protected void FillListBox()
        {
            db_base db = db_base.getDBhandle();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT [T_PERSONINFO].[NUMBER],[T_PERSONINFO].[NAME] FROM [T_PERSONINFO],[T_PARTY],[T_BRANCH] WHERE [T_PARTY].[PERSONID]=[T_PERSONINFO].[ID] AND [T_BRANCH].[ID]=[T_PARTY].[BRANCH_ID] AND [T_BRANCH].[BR_NAME]=@BR_NAME AND NOT EXISTS (SELECT [T_PERSONINFO].[NUMBER],[T_PERSONINFO].[NAME] FROM [T_POST],[T_PERSONINFO],[T_PARTY],[T_BRANCH],[T_SUBJECT] WHERE [SUBJECT]=@SUBJECT AND [T_POST].[STUDENT_ID]=[T_PERSONINFO].[NUMBER] AND [T_PARTY].[PERSONID]=[T_PERSONINFO].[ID] AND [T_BRANCH].[ID]=[T_PARTY].[BRANCH_ID] AND [T_BRANCH].[BR_NAME]=@BR_NAME AND [SUBJECT_ID]=[T_SUBJECT].[ID])";
            cmd.Parameters.Add("@SUBJECT", DropDownList2.SelectedValue);
            cmd.Parameters.Add("@BR_NAME", DropDownList1.SelectedValue);
            DataTable dt = db.GetDataTableByCmd(cmd);
            ListBox1.DataSource = dt;
            ListBox1.DataTextField = "NAME";
            ListBox1.DataValueField = "NAME";
            ListBox1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedItem != null && DropDownList2.SelectedItem != null)
            {
                FillListBox();
            }
        }
    }
}
